﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LDSP.BRL
{
    public class BaseEnterprisePage : BaseValidUserPage
    {
        protected List<int> m_allowedCatalogIDs = null;
        protected List<int> AllowedCatalogIDs
        {
            get
            {
                if (m_allowedCatalogIDs == null)
                {
                    m_allowedCatalogIDs = new List<int>();
                    for (int i = 0; i < EnterpriseUserCatalogs.Count; i++)
                    {
                        m_allowedCatalogIDs.Add(EnterpriseUserCatalogs[i].CatalogID);
                    }
                }
                return m_allowedCatalogIDs;
            }
        }

        protected List<R_EnterpriseCatalog> m_enterpriseUserCatalogs = null;
        protected List<R_EnterpriseCatalog> EnterpriseUserCatalogs
        {
            get
            {
                if (m_enterpriseUserCatalogs == null)
                {
                    m_enterpriseUserCatalogs = new R_EnterpriseCatalog().GetListByEnterpriseID(LoginUser.EnterpriseID);
                }
                return m_enterpriseUserCatalogs;
            }
        }

        public override void RedirectBefore()
        {
            if (LoginUser == null
                || (LoginUser.UserType != EnterpriseUserType.企业管理员
                && LoginUser.UserType != EnterpriseUserType.普通用户))
            {
                this.GoToLogin();
            }
            else
            {
                //用户被删除后，即使已登录了也退出
                LoginUser.GetProperties(LoginUser.ID);
                if (LoginUser.ID == 0 || LoginUser.IsValid == false)
                {
                    this.GoToLogin();
                }
            }
        }

        public override void GoToLogin(bool autoLoginWithCookie)
        {
            if (autoLoginWithCookie)
            {
                this.Response.Write("<script>window.parent.location.href='/UI/Login.aspx';</script>");
            }
            else
            {
                this.Response.Write("<script>window.parent.location.href='/UI/Login.aspx?FOBID_AUTO_LOGIN=true';</script>");
            }
        }

        /// <summary>
        /// 是否允许系统管理员
        /// </summary>
        /// <param name="redirect">是否重定向到默认页</param>
        /// <returns></returns>
        protected virtual bool AllowEnterpriseUser(bool redirect = true)
        {
            if (LoginUser != null)
            {
                return true;
            }
            else
            {
                this.JSAlert("您没有访问权限！", true);
                if (redirect)
                {
                    this.JSRedirect("MyPage.aspx", false, true);
                }
                return false;
            }
        }

        /// <summary>
        /// 将栏目按照企业定义的顺序排序
        /// </summary>
        /// <param name="list"></param>
        protected List<Catalog> SortCatalogList(List<Catalog> list)
        {
            List<Catalog> res = new List<Catalog>();
            for (int i = 0; i < AllowedCatalogIDs.Count; i++)
            {
                for (int j = 0; j < list.Count; j++)
                {
                    if (AllowedCatalogIDs[i] == list[j].ID)
                    {
                        res.Add(list[j]);
                        list.RemoveAt(j);
                        j--;
                        break;
                    }
                }
            }
            return res;
        }

        /// <summary>
        /// 检查用户是否有当前catalog的权限
        /// </summary>
        /// <param name="catalogID"></param>
        /// <returns></returns>
        protected bool HaveCatalogPermission(int catalogID)
        {
            return catalogID == 0 || AllowedCatalogIDs.Contains(catalogID);
        }

        /// <summary>
        /// 根据catalogID获取当前登录用户的EnterpriseCatalog
        /// </summary>
        /// <param name="catalogID"></param>
        /// <returns></returns>
        protected R_EnterpriseCatalog GetEnterpriseCatalogForLoginUserByCatalogID(int catalogID)
        {
            for (int i = 0; i < EnterpriseUserCatalogs.Count; i++)
            {
                if (EnterpriseUserCatalogs[i].CatalogID == catalogID)
                {
                    return EnterpriseUserCatalogs[i];
                }
            }
            return null;
        }

        /// <summary>
        /// 当前选中用户列表
        /// </summary>
        /// <param name="pageID"></param>
        /// <param name="val"></param>
        /// <param name="remove"></param>
        /// <returns></returns>
        protected string UserList(string pageID, string val = null, bool remove = false)
        {
            string key = pageID + "UserList";
            if (remove)
            {
                this.RemoveSessionData(key);
                return "";
            }
            if (val != null)
            {
                this.SetSessionData(key, val);
            }
            object res = this.GetSessionData(key);
            return res == null ? "" : res.ToString();
        }

        /// <summary>
        /// 获取字符串形式的责任人列表
        /// </summary>
        /// <param name="ppList"></param>
        /// <returns></returns>
        protected string GetPrincipleListStr(List<R_PlanPrinciple> ppList)
        {
            string priciples = string.Empty;
            List<string> nameList = new List<string>();
            for (int i = 0; i < ppList.Count; i++)
            {
                int id = ppList[i].UserID;
                EnterpriseUser ett = new EnterpriseUser();
                ett.GetProperties(id);
                nameList.Add(ett.Name);
            }
            priciples = this.CombineStrS(nameList);

            return priciples;
        }

        /// <summary>
        /// 适用法规的公共信息
        /// </summary>
        /// <param name="pageID"></param>
        /// <param name="val"></param>
        /// <param name="remove"></param>
        /// <returns></returns>
        protected PublicFavoriteInfo PublicFavoriteInfo(string pageID, PublicFavoriteInfo val = null, bool remove = false)
        {
            string key = pageID + "InvalidList";
            if (remove)
            {
                this.RemoveSessionData(key);
                return null;
            }
            if (val != null)
            {
                this.SetSessionData(key, val);
                return val;
            }
            object res = this.GetSessionData(key);
            return res == null ? new PublicFavoriteInfo() : (PublicFavoriteInfo)res;
        }

        /// <summary>
        /// 添加适用法规信息给具体公司
        /// </summary>
        /// <param name="find"></param>
        /// <param name="ett"></param>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        protected R_EnterpriseCodification AddFavirote(bool find, Codification ett, Enterprise enterprise)
        {
            find = true;
            R_EnterpriseCodification tmp = new R_EnterpriseCodification();
            //已存在的适用关系则不添加进去（处理并发情况）
            if (!tmp.Exist(enterprise.ID, ett.ID))
            {
                tmp.DocID = ett.ID;
                tmp.EnterpriseID = enterprise.ID;
                tmp.OwnerID = LoginUser.ID;
                tmp.FavoriteDT = DateTime.Now;
                PublicFavoriteInfo pi = this.PublicFavoriteInfo(this.PageID);
                tmp.Comment = pi.Comment;
                tmp.Principal = pi.Principal;
                tmp.Priority = pi.Priority;
                tmp.Name = ett.Summary;
                tmp.KeyItems = ett.KeyItems;
                tmp.Add();
            }

            //记录一次法规/标准的查看.
            if (tmp.Codification != null)
            {
                tmp.Codification.ViewTimes++;
                tmp.Codification.Update();
            }

            return tmp;
        }
    }
}